<?php

namespace App\Http\Controllers\Api;

use App\Libs\WeChat\WxSmallClient;
use App\Models\member;
use App\Models\user;
use App\Models\user_formid;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Crypt;
use Illuminate\Support\Facades\Redis;
use Illuminate\Support\Facades\Session;
use Ixudra\Curl\Facades\Curl;

class TestController extends Controller
{
    public function sendtemp(Request $requests)
    {
        $id = $requests->json('id');
        $user = user::where('id', $id)->first();
        $template_id = 't9XzxP1iF_XunJHqENE1Ontqt6bOMYHAFvfoqoVcZE8';
        $page = 'pages/index/index';
        $formId = user_formid::select('id', 'form_id')->where(['openid'=>$user->openid])->orderBy('id', 'asc')->first();
        if (!$formId) {
            fun_respon(0, '为找到推送模板的formId');
        }
        //示例数据根据消息模板填充
        $data = array(
            'keyword1'=>array('value'=>'您参与的翻脸活动可以领取礼物啦','color'=>'#7167ce'),
            'keyword2'=>array('value'=>'鹰嘴脆一盒','color'=>'#7167ce'),
            'keyword3'=>array('value'=>'PavoMea','color'=>'#7167ce')
        );
        $res = WxSmallClient::sendTemplate($user->openid, $data, $formId->form_id, $template_id);
        var_dump($res);
        // 删除用过的form_id
        user_formid::where('id', $formId->id)->delete();
    }

    public function send(){
        $user_info = user::take(2000)->skip(25000)->orderBy('id','ASC')->get(['openid'])->toArray();
        //var_dump($user_info);exit;
        if($user_info){
            $data = array(
                'keyword1'=>array('value'=>'零食库悄悄开张，内有隐藏剧情','color'=>'#7167ce'),
                'keyword2'=>array('value'=>'实验室神秘研发，全球最有情趣的零食初次预售，还有全场优惠哦','color'=>'#7167ce'),
            );
            $template_id = 'GcDsZ0ALQCGFmPXjVZtSD-3AZNvErBainA18W76_9bA';
            foreach ($user_info as $v){
                $formId = user_formid::lastformid($v['openid']);
                if (!$formId) {
                    savelog('no_push_user', 'openid=' . $v['openid']);
                    continue;
                }
                $res = WxSmallClient::sendTemplate($v['openid'], $data, $formId->form_id, $template_id,'pages/home/home');
                if (!isset($res['errcode']) || $res['errcode'] != 0) {
                    savelog('push_err_log', json_encode($res).'openid='.$v['openid'].'&form_id='.$formId->form_id);
                }
                // 删除用过的form_id
                user_formid::where('id', $formId->id)->delete();
                echo 'ok';
            }
        }

    }

    public function test()
    {
        var_dump(date('Y-m-d H:s:i', time()));exit;

        $url = "https://api.weixin.qq.com/card/code/decrypt?access_token=11_YcLR93j5pfvZooyQv16OLivdE26i1FONvgjfIKbYq7FY8EWSbkz02o363FwbqTejIk1xdJs8C8qBdaYhNE8t7JpQby3EQGff332HA0gebzIsTFhth9jR9jbRMozLLXswMJTubNjN41s1yYmhVNOhACAJBC";
        $res = Curl::to( $url )
            ->withContentType('application/json')
            ->withData(json_encode(['encrypt_code'=>'1jmpyogY9DPMmJ%2BFqhfNCQHoJWUy36Lu8zBe83K2CeE%3D']))
            ->post();
        var_dump($res);
    }


    public function getToken(){
        $openid = 'onZZo5DpHaxlEfDbNPoNG9rtZw7w';
        $token = Crypt::encrypt(['openid'=>$openid]);
        Redis::setex('token_'.$openid, 14400, $token);
        var_dump($token);
    }

    public function gettest(){
//            $token = Crypt::encrypt(['openid'=>'oyybT5BCvoLQU4WSKp-8d6UStm_Y']);
//            Redis::setex('token_oyybT5BCvoLQU4WSKp-8d6UStm_Y', 14400, $token);
//            var_dump($token);
           //WxSmallClient::createdMemberCard();
        $res = WxSmallClient::getMember('189016972589');
        //var_dump($res);
        $arr['nickname'] = $res->nickname;
        $arr['membership_number'] = $res->membership_number;
        $arr['bonus'] = $res->bonus;
        $arr['sex'] = $res->sex=='MALE'?1:0;
        //$arr['user_card_status'] = array_search($res->user_card_status,self::status);
        member::insert($arr);
//        //计算消费积分
//        //$consumer_total = $this->getbonus(['openid'=>$openid,'species'=>1,'type'=>1]);
//        //计算总积分
//        //$total = $this->getbonus(['openid'=>$openid,'type'=>1]);
//        //$level = get_level($total,$consumer_total);
//        //$arr['level'] = $member_info['gender']==1?self::LEVEL_MAN[$level]:self::LEVEL_WOMAN[$level];
//        //$arr['level'] = $level;
//        $arr['crown_num'] = intval($res->bonus/10);
    }

}
